'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
    ------
    
    About: Build modules widget used in the Rig Tools window.
'''

'''
============================================================
--->>   IMPORT MODULES
============================================================
'''
import masdevallia.settings.PathConstants as PathConstants
import masdevallia.utility.FileSystem as FileSystem
import masdevallia.utility.WindowUtility as WinUtility
import masdevallia.windows.InfoTile as InfoTile
import masdevallia.utility.RigControls as RigControls

from PySide import QtCore
from PySide import QtGui

'''
============================================================
--->>   GLOBAL VARIABLES
============================================================
'''
form_class, base_class = WinUtility.loadUiType( PathConstants.WIDGET_BUILD_MODULES_GUI )

'''
============================================================
--->>   BUILD MODULES CLASS
============================================================
'''
class BuildModules( form_class, base_class ):
    def __init__( self, parent=None ):
        super( BuildModules, self).__init__( parent )
        self.setupUi( self )
        self.setObjectName( 'Build_Modules_Widget' )
        
        # List containing InfoTiles.
        self.rootWidgets = []
        self.jointWidgets = []
        self.spacerWidgets = []
        self.controlWidgets = []
        
        self.initUI()
    
    def initUI( self ):        
        # Set each tab's QFrame background to black. These frames are defined in
        # the UI file.
        
        self.RootsScrollLayout.setAlignment( QtCore.Qt.AlignTop )
        self.JointsScrollLayout.setAlignment( QtCore.Qt.AlignTop )
        self.SpacersScrollLayout.setAlignment( QtCore.Qt.AlignTop )
        self.ControlsScrollLayout.setAlignment( QtCore.Qt.AlignTop )
        
        CSS_FRAME = '''
            QFrame {
                background-color: black;
            }'''
        self.RootsScrollFrame.setStyleSheet( CSS_FRAME )
        self.JointsScrollFrame.setStyleSheet( CSS_FRAME )
        self.SpacersScrollFrame.setStyleSheet( CSS_FRAME )
        self.ControlsScrollFrame.setStyleSheet( CSS_FRAME )
        
        # Refresh the tiles on all tabs.
        self.refreshInfoTiles()
        
        # Do the connections.
        self.connections()
        
    def connections( self ):
        self.SaveXML.clicked.connect( self.saveSelectedToXML )
        self.Refresh.clicked.connect( self.refreshInfoTiles )
        self.tabWidget.currentChanged.connect( self.refreshInfoTiles )
                
    def saveSelectedToXML( self ):
        RigControls.saveSelectedToXML()
        
    def refreshInfoTiles( self ):
        # Fill the GUI with Info Tiles
        if self.tabWidget.currentIndex() == 0:# roots
            directory = PathConstants.PATH_MODULE_ROOTS
            WinUtility.clearLayout( self.RootsScrollLayout )
            self.rootWidgets = []
        elif self.tabWidget.currentIndex() == 1:# joints
            directory = PathConstants.PATH_MODULE_JOINTS
            WinUtility.clearLayout( self.JointsScrollLayout )
            self.jointWidgets = []
        elif self.tabWidget.currentIndex() == 2:# spacers
            directory = PathConstants.PATH_MODULE_SPACERS
            WinUtility.clearLayout( self.SpacersScrollLayout )
            self.spacerWidgets = [] 
        elif self.tabWidget.currentIndex() == 3:# controls
            directory = PathConstants.PATH_MODULE_CONTROLS
            WinUtility.clearLayout( self.ControlsScrollLayout )
            self.controlWidgets = [] 
        
        # Get the controls XML files.
        files = FileSystem.searchSubDir( directory, 'xml' )
        
        for key in files.keys():
            for f in files[key]:
                nameLabel = f.split( '.' )[0]
                pathSplit = key.split( '\\' )
                
                subtypeLabel = pathSplit[-1]
                
                del pathSplit[-1]
                typeLabel = pathSplit[-1] 
                
                filePath = key + '\\' + f
                
                # Create the widget.
                tile = InfoTile.InfoTile( nameLabel = nameLabel,
                                          typeLabel = typeLabel,
                                          subtypeLabel = subtypeLabel,
                                          filePath = filePath )
                
                # Add the widgets to the correct layout.
                if self.tabWidget.currentIndex() == 0:# roots
                    self.rootWidgets.append( tile )
                    self.RootsScrollLayout.addWidget( self.rootWidgets[-1] )
                elif self.tabWidget.currentIndex() == 1:# joints
                    self.jointWidgets.append( tile )
                    self.JointsScrollLayout.addWidget( self.jointWidgets[-1] )
                elif self.tabWidget.currentIndex() == 2:# spacers
                    self.spacerWidgets.append( tile )
                    self.SpacersScrollLayout.addWidget( self.spacerWidgets[-1] )
                elif self.tabWidget.currentIndex() == 3:# controls
                    self.controlWidgets.append( tile ) 
                    self.ControlsScrollLayout.addWidget( self.controlWidgets[-1] )